<?php

namespace App\Services;

use App\Repositories\Company\CompanyRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class CompanyService
{
    private $companyRepository;

    public function __construct(CompanyRepository $companyRepository)
    {
        $this->companyRepository = $companyRepository;
    }

    public function createCompany($uid, Request $request)
    {
        $rule = $this->companyRepository->getCreateCompanyMustParamRules();
        $rule = array_merge($rule, ['type' => 'required|integer']);

        $data = $request->all();
        $data = array_merge($data, ['uid' => $uid]);

        Validator::make($data, $rule)->validate();

        $companyOptionParam = $this->companyRepository->getCreateCompanyOptionReloadNeedParams();
        $companyParam = $this->companyRepository->getCreateCompanyReloadNeedParams();

        $companyData = $companyOptionData = [];
        foreach ($companyParam as $item) {
            $companyData[$item] = $data[$item];
        }
        foreach ($companyOptionParam as $item){
            if (empty($dta[$item])) $companyOptionData[$item] = $data['item'];
        }

        $this->companyRepository->addCompany($data['uid'], $data['type'], $companyData, $companyOptionData);
    }
}